package net.ibizsys.paas.ctrlmodel;

import net.ibizsys.paas.control.grid.IGrid;
import net.ibizsys.paas.control.grid.IGridDataItem;
import net.ibizsys.paas.controller.ViewController;
import net.ibizsys.paas.core.CallResult;
import net.ibizsys.paas.core.IActionContext;
import net.ibizsys.paas.core.ISystem;
import net.ibizsys.paas.data.ISimpleDataObject;
import net.ibizsys.paas.datamodel.DataItemModel;
import net.ibizsys.paas.demodel.IDataEntityModel;
import net.ibizsys.paas.util.StringHelper;
import net.ibizsys.paas.web.IWebContext;

/**
 * 表格数据项模型
 * @author lionlau
 *
 */
public class GridDataItemModel extends DataItemModel implements IGridDataItem
{
	private IGrid iGrid = null;
	private  boolean bDataAccessAction = false;
	private IGridModel iGridModel = null;
	public GridDataItemModel(){
		
	}
	
	/**
	 * 初始化
	 * @param iGrid
	 * @throws Exception
	 */
	public void init(IGrid iGrid) throws Exception{
		this.setGrid(iGrid);
		this.onInit();
	}
	
	

	/**
	 * 获取表格对象
	 * @return the iGrid
	 */
	protected IGrid getGrid()
	{
		return iGrid;
	}
	
	/**
	 * 获取表格模型对象
	 * @return
	 */
	protected IGridModel getGridModel(){
		return this.iGridModel;
	}

	/**
	 * 设置表格对象
	 * @param iGrid the iGrid to set
	 */
	protected void setGrid(IGrid iGrid)
	{
		this.iGrid = iGrid;
		if(this.iGrid == null){
			this.iGridModel = null; 
		}
		else
			if(this.iGrid instanceof IGridModel){
				this.iGridModel = (IGridModel)this.iGrid;
			}
			
	}

	
	
	/* (non-Javadoc)
	 * @see net.ibizsys.paas.data.impl.DataItemImpl#getCurSystem(net.ibizsys.paas.core.IActionContext)
	 */
	@Override
	public ISystem getCurSystem(IActionContext iActionContext ) throws Exception
	{
		return this.getGrid().getDataEntity().getSystem();
	}

	/* (non-Javadoc)
	 * @see net.ibizsys.paas.control.grid.IGridDataItem#isDataAccessAction()
	 */
	@Override
	public boolean isDataAccessAction() {
		return this.bDataAccessAction;
	}

	/**
	 * 设置界面操作数据项
	 * @param bDataAccessAction
	 */
	public void setDataAccessAction(boolean bDataAccessAction){
		this.bDataAccessAction = bDataAccessAction;
	}

	/* (non-Javadoc)
	 * @see net.ibizsys.paas.data.impl.DataItemImpl#getValue(net.ibizsys.paas.web.IWebContext, java.lang.Object)
	 */
	@Override
	public Object getValue(IWebContext iWebContext, Object object) throws Exception {
		if(isDataAccessAction()){
			if(this.getGridModel() == null){
				throw new Exception(StringHelper.format("当前表格模型对象无效"));
			}
			
			String strKeyName = this.getDataItemParams()[0].getName();
			String strAccessAction = this.getDataItemParams()[1].getName();
			
			String strRet = "0";
			
			if(object instanceof ISimpleDataObject){
				Object objKey = ((ISimpleDataObject)object).get(strKeyName);
				CallResult callResult = this.getGridModel().getDEModel().getDEDataAccMgr().test(iWebContext, objKey, strAccessAction,true);
				if(callResult.isOk()){
					strRet = "1";
				}
			}
			
			return strRet;
		}
		else{
			return super.getValue(iWebContext, object);
		}
		
	}
	
	
	
	/* (non-Javadoc)
	 * @see net.ibizsys.paas.data.impl.DataItemImpl#getDEModel()
	 */
	@Override
	protected IDataEntityModel  getDEModel()throws Exception
	{
		return ViewController.getCurrent().getDEModel();
	}
}
